package com.fhc.base

import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.biometric.BiometricManager
import androidx.biometric.BiometricPrompt
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity

class BiometricActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Surface(
                modifier = Modifier.fillMaxSize(),
                color = MaterialTheme.colorScheme.background
            ) {
                BiometricExampleScreen(
                    onAuthSuccess = {
                        Toast.makeText(this, "生物识别成功!", Toast.LENGTH_SHORT).show()
                    },
                    modifier = Modifier
                )
            }
        }
    }
}

@Composable
fun BiometricExampleScreen(
    onAuthSuccess: () -> Unit,
    modifier: Modifier = Modifier
) {
    val context = LocalContext.current
    
    // 检查设备是否支持生物识别
    val biometricManager = androidx.compose.runtime.remember { BiometricManager.from(context) }
    val canAuthenticate = androidx.compose.runtime.remember {
        biometricManager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_STRONG) == 
                BiometricManager.BIOMETRIC_SUCCESS
    }
    
    Column(
        modifier = modifier
            .fillMaxSize()
            .padding(16.dp),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center
    ) {
        Text(
            text = "生物识别示例",
            style = MaterialTheme.typography.headlineMedium,
            modifier = Modifier.padding(bottom = 32.dp)
        )
        
        if (canAuthenticate) {
            Button(
                onClick = {
                    // 创建生物识别提示信息
                    val promptInfo = BiometricPrompt.PromptInfo.Builder()
                        .setTitle("生物识别验证")
                        .setSubtitle("请验证您的身份以继续")
                        .setNegativeButtonText("取消")
                        .build()
                    
                    // 创建回调
                    val biometricCallback = object : BiometricPrompt.AuthenticationCallback() {
                        override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
                            // 验证成功
                            onAuthSuccess()
                        }
                        
                        override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
                            // 验证错误
                            Toast.makeText(context, "验证错误: $errString", Toast.LENGTH_SHORT).show()
                        }
                        
                        override fun onAuthenticationFailed() {
                            // 验证失败
                            Toast.makeText(context, "验证失败，请重试", Toast.LENGTH_SHORT).show()
                        }
                    }
                    
                    // 启动生物识别验证
                    val biometricPrompt = BiometricPrompt(
                        context as FragmentActivity,
                        ContextCompat.getMainExecutor(context),
                        biometricCallback
                    )
                    
                    biometricPrompt.authenticate(promptInfo)
                }
            ) {
                Text("启动生物识别验证")
            }
        } else {
            Text(
                text = "您的设备不支持生物识别或未设置生物识别信息",
                style = MaterialTheme.typography.bodyLarge
            )
        }
    }
}

@Preview(showBackground = true)
@Composable
fun BiometricExamplePreview() {
    Surface(
        modifier = Modifier.fillMaxSize(),
        color = MaterialTheme.colorScheme.background
    ) {
        BiometricExampleScreen(
            onAuthSuccess = {},
            modifier = Modifier
        )
    }
}